/**
 * 获取指定接口的信息
 * @param {Number} index_  接口索引
 * @returns {Object} intItem   会议列表: { url: '', istoken: true, desc: '' }
 */
function getInterfaceInfo(index_) {
  //   return $interfaceName[Object.keys($interfaceName)[index_]]
  return Object.keys($interfaceName)[index_]
}

function getInterfaceIndex(interfaceIndexData) {
  const tempData = interfaceIndexData.split(' ')
  if (tempData.length > 1) {
    return tempData[1]
  } else {
    return -1
  }
}

function setPagination(store, DBData, interfaceIndex) {
  // x-author: "interfaceName 12"
  const index_ = getInterfaceIndex(interfaceIndex)
  const itemName = getInterfaceInfo(index_)
  //   console.log('setPagination =>', store, DBData)
  if (!DBData || !DBData.pageNum) {
    return
  }
  const {
    pageNum,
    pageSize,
    pages,
    total
  } = DBData
  // store.commit('addPagination') interfaceName, paginationObj_ = { pageNum: 1, pages: 0, pageSize: 10, isRequest: false }
  if (pageNum && pageSize && pages) {
    store.commit('addPagination', {
      interfaceName: itemName,
      Pagination: {
        pageNum: pageNum,
        pages: pages,
        pageSize: pageSize,
        isRequest: false
      }
    })
  }
}

function getPagination(store, interfaceIndex) {
  const index_ = getInterfaceIndex(interfaceIndex)
  const itemName = getInterfaceInfo(index_)
  let itemPaginationData = store.getters.getPagination[itemName]
  //   isRequest: false pageNum: 1 pageSize: 10 pages: 1
  //   console.log('itemPaginationData itemPaginationData =>', itemPaginationData)
  if (itemPaginationData) {
    // 暂时 pageSize = 100
    itemPaginationData.pageSize = 100
    itemPaginationData.pages = 1
    if (itemPaginationData.pageNum < itemPaginationData.pages && itemPaginationData.isRequest) {
      itemPaginationData.pageNum = itemPaginationData.pageNum + 1
    } else {
      itemPaginationData.pageNum = itemPaginationData.pages
    }
  }

  return itemPaginationData
}

export default (funName, store, DBData, interfaceIndex) => {
  // "total": 3,  总记录数
  // "pages": 1,  总页数
  // "pageSize": 10, 每页数量（10条）
  // "list": [],
  // "pageNum": 1  当前页
  if (funName === 'setPagination') {
    setPagination(store, DBData, interfaceIndex)
  }
  if (funName === 'getPagination') {
    return getPagination(store, interfaceIndex)
  }
}
